package id.net.iconpln.bb.client.view;

import id.net.iconpln.bb.client.data.TestData;
import id.net.iconpln.bb.client.model.Customer;
import id.net.iconpln.bb.client.model.Pemasok;
import id.net.iconpln.bb.client.model.Stock;
import id.net.iconpln.bb.client.model.Waktu;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.form.CheckBoxGroup;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.extjs.gxt.ui.client.widget.table.NumberCellRenderer;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.Element;

public class PageBbRencBulan extends LayoutContainer {
	private FormBinding formBindings;  
	private FormData formData; 
	private Grid<Stock> grid;
	
	  @Override  
	  protected void onRender(Element parent, int index) {  
	    super.onRender(parent, index);  
	    setLayout(new FitLayout());
	    
	    formData = new FormData("-20"); 
	    ContentPanel cp = new ContentPanel();  
	  
	    cp.setHeading("Rencana Pengiriman Batubara");  
	    cp.setFrame(true);  
	    cp.setSize(900, 400);  
	    cp.setLayout(new RowLayout(Orientation.HORIZONTAL));  
	  
	    grid = createGrid();  
	    grid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);  
	    grid.getSelectionModel().addListener(Events.SelectionChange,  
	        new Listener<SelectionChangedEvent<Stock>>() {  
	          public void handleEvent(SelectionChangedEvent<Stock> be) {  
	            if (be.getSelection().size() > 0) {  
	              formBindings.bind((ModelData) be.getSelection().get(0));  
	            } else {  
	              formBindings.unbind();  
	            }  
	          }  
	        });  	     
	  
	    FormPanel panel = createForm();  
	    formBindings = new FormBinding(panel, true);  
	    formBindings.setStore((Store) grid.getStore());  
	  
	    cp.add(panel, new RowData(.4, 1));
	    cp.add(grid, new RowData(.6, 1));
	    
	    
//		setScrollMode(Scroll.AUTO);		
	    add(cp, new FitData(5));  
	  }  
	  
	  private FormPanel createForm() {  
	    FormPanel panel = new FormPanel();	    
	    panel.setHeaderVisible(false);
	    
	    FieldSet fieldSet = new FieldSet();  
	    fieldSet.setHeading("Data pengiriman");  
	    fieldSet.setCollapsible(false);
	    
	  
	    FormLayout layout = new FormLayout();  
	    layout.setLabelWidth(120);  
	    fieldSet.setLayout(layout);  

	    TextField<String> noPengiriman = new TextField<String>();  
	    noPengiriman.setName("noPengiriman");  
	    noPengiriman.setFieldLabel("No Pengiriman");    
	    fieldSet.add(noPengiriman, formData);	  	  
	    
	    List<Pemasok> c = TestData.getPemasoks();  
//	    Collections.sort(c, new Comparator<Pemasok>() {  
//	      public int compare(Pemasok arg0, Pemasok arg1) {  
//	        return arg0.getName().compareTo(arg1.getId());  
//	      }  
//	    });  
	    
	    ListStore<Pemasok> store = new ListStore<Pemasok>();  
	    store.add(c);  
	  
	    ComboBox<Pemasok> pemasok = new ComboBox<Pemasok>();  
	    pemasok.setFieldLabel("Pemasok");  
	    pemasok.setDisplayField("pemasok");
//	    pemasok.setName("pemasok");
	    pemasok.setTriggerAction(TriggerAction.ALL);  
	    pemasok.setStore(store);  
	    fieldSet.add(pemasok, formData);  
	     
	    TextField<String> noKontrak = new TextField<String>();	    
	    noKontrak.setName("noKontrak");  
	    noKontrak.setFieldLabel("No Kontrak");    
	    fieldSet.add(noKontrak, formData);

	    
//	    NumberField muatan = new NumberField();
	    TextField<String> muatan = new TextField<String>();
	    muatan.setName("muatan");  
	    muatan.setFieldLabel("Muatan Kpl");	    
	    fieldSet.add(muatan, formData);

//	    NumberField tonase = new NumberField();  
	    TextField<String> tonase = new TextField<String>();
	    tonase.setName("tonase");  
	    tonase.setFieldLabel("Vol Kirim");	    
	    fieldSet.add(tonase, formData);

	    TextField<String> surveyor = new TextField<String>();	    
	    surveyor.setName("surveyor");  
	    surveyor.setFieldLabel("Nama Surveyor");    
	    fieldSet.add(surveyor, formData);	    	    


	    TextField<String> berangkat = new TextField<String>();	    
	    berangkat.setName("berangkat");  
	    berangkat.setFieldLabel("Tgl Keberangkatan");    
	    fieldSet.add(berangkat, formData);	    	    	    
	    
	    TextField<String> eta = new TextField<String>();	    
	    eta.setName("eta");  
	    eta.setFieldLabel("ETA");    
	    fieldSet.add(eta, formData);
	    	    
	    panel.add(fieldSet);  
	    
  
	    
	    FieldSet fieldSet2 = new FieldSet();  
	    fieldSet2.setHeading("Dokumen Terkait");  
	    fieldSet2.setCollapsible(false);

	    FormLayout layout2 = new FormLayout();  
	    layout2.setLabelWidth(50);  
	    fieldSet2.setLayout(layout2);  

	    CheckBox check1 = new CheckBox();  
	    check1.setBoxLabel("Manifest");  
	  
	    CheckBox check2 = new CheckBox();  
	    check2.setBoxLabel("Sertifikat kalori");  
	    check2.setValue(true);  
	  
	    CheckBox check3 = new CheckBox();  
	    check3.setBoxLabel("Sertifikat Weight");  
	  
	    CheckBoxGroup checkGroup = new CheckBoxGroup();  
	    checkGroup.setFieldLabel("Lampiran");  
	    checkGroup.add(check1);  
	    checkGroup.add(check2);  
	    checkGroup.add(check3);  
	    fieldSet2.add(checkGroup, formData); 
	  
	    panel.add(fieldSet2);

	    ContentPanel p = new ContentPanel();
	    p.setHeaderVisible(false);	    
	    p.setButtonAlign(HorizontalAlignment.RIGHT);  
	    p.addButton(new Button("Reset", new SelectionListener<ButtonEvent>() {  
	      @Override  
	      public void componentSelected(ButtonEvent ce) {  
	        grid.getStore().rejectChanges();  
	      }  
	    }));  
	    p.addButton(new Button("Submit", new SelectionListener<ButtonEvent>() {  
	      @Override  
	      public void componentSelected(ButtonEvent ce) {  
	        grid.getStore().commitChanges();  
	      }  
	    }));  

	    panel.add(p);
	    
	    return panel;  
	  }  
	  
	  private Grid<Stock> createGrid() {  
	    final NumberFormat currency = NumberFormat.getCurrencyFormat();  
	    final NumberFormat number = NumberFormat.getFormat("0.00");  
	    final NumberCellRenderer<Grid<Stock>> numberRenderer = new NumberCellRenderer<Grid<Stock>>(  
	        currency);  
	  
	    GridCellRenderer<Stock> change = new GridCellRenderer<Stock>() {  
	  
	      public String render(Stock model, String property, ColumnData config, int rowIndex,  
	          int colIndex, ListStore<Stock> store, Grid<Stock> grid) {  
	        double val = (Double) model.get(property);  
	        String style = val < 0 ? "red" : "green";  
	        return "<span style='color:" + style + "'>" + number.format(val) + "</span>";  
	      }  
	    };  
	  
	    GridCellRenderer<Stock> gridNumber = new GridCellRenderer<Stock>() {  
	      public String render(Stock model, String property, ColumnData config, int rowIndex,  
	          int colIndex, ListStore<Stock> store, Grid<Stock> grid) {  
	        return numberRenderer.render(null, property, model.get(property));  
	      }  
	    };  
	  
	    List<ColumnConfig> configs = new ArrayList<ColumnConfig>();  

	    ColumnConfig column = new ColumnConfig();
	    
	    column = new ColumnConfig();  
	    column.setId("noPengiriman");  
	    column.setHeader("No Pengiriman");  
//	    column.setAlignment(HorizontalAlignment.LEFT);  
	    column.setWidth(150);  
//	    column.setRenderer(gridNumber);  
	    configs.add(column);  

	    
	    column = new ColumnConfig();  
	    column.setId("pemasok");  
	    column.setHeader("Pemasok");  
	    column.setWidth(150);  
	    configs.add(column);  
	  
	    column = new ColumnConfig();  
	    column.setId("noKontrak");  
	    column.setHeader("No Kontrak");  
	    column.setWidth(100);  
	    configs.add(column);  
	  
	    column = new ColumnConfig();  
	    column.setId("muatan");  
	    column.setHeader("Muatan Kpl");  
//	    column.setAlignment(HorizontalAlignment.RIGHT);  
	    column.setWidth(100);  
//	    column.setRenderer(gridNumber);  
	    configs.add(column);  
	  
	    column = new ColumnConfig();  
	    column.setId("tonase");  
	    column.setHeader("Vol (Ton)");  
//	    column.setAlignment(HorizontalAlignment.RIGHT);  
	    column.setWidth(100);  
//	    column.setRenderer(gridNumber);  
	    configs.add(column);
	    
	    column = new ColumnConfig();  
	    column.setId("surveyor");  
	    column.setHeader("Surveyor");  
//	    column.setAlignment(HorizontalAlignment.RIGHT);  
	    column.setWidth(150);  
//	    column.setRenderer(gridNumber);  
	    configs.add(column);
	    
	    column = new ColumnConfig();  
	    column.setId("berangkat");  
	    column.setHeader("Tgl kirim");  
//	    column.setAlignment(HorizontalAlignment.RIGHT);  
	    column.setWidth(75);  
//	    column.setRenderer(gridNumber);  
	    configs.add(column);
	    
	    column = new ColumnConfig();  
	    column.setId("eta");  
	    column.setHeader("ETA");  
//	    column.setAlignment(HorizontalAlignment.RIGHT);  
	    column.setWidth(100);  
//	    column.setRenderer(gridNumber);  
	    configs.add(column);
	    
	    ListStore<Stock> store = new ListStore<Stock>();  
	    store.setMonitorChanges(true);  
	    store.add(TestData.getPengiriman());
	  
	    ColumnModel cm = new ColumnModel(configs);  
	  
	    Grid<Stock> grid = new Grid<Stock>(store, cm);  
	    grid.getView().setEmptyText("sdfd");  
	    grid.setBorders(false);  
//	    grid.setAutoExpandColumn("pemasok");  
	    grid.setBorders(true);  
	  
	    return grid;  
	  }  
}
